package com.xxd.spring.boot.demo.wubolin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxd.spring.boot.demo.wubolin.entity.StudentCurriculumFraction;
import com.xxd.spring.boot.demo.wubolin.exception.MyException;
import com.xxd.spring.boot.demo.wubolin.mapper.StudentCurriculumFractionMapper;
import com.xxd.spring.boot.demo.wubolin.service.StudentCurriculumFractionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StudentCurriculumFractionServiceImpl implements StudentCurriculumFractionService {

    @Autowired
    private StudentCurriculumFractionMapper studentCurriculumFractionMapper;

    @Override
    public int add(StudentCurriculumFraction studentCurriculumFraction) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", studentCurriculumFraction.getId());
        StudentCurriculumFraction fraction = studentCurriculumFractionMapper.selectOne(queryWrapper);
        if (fraction != null) {
            throw new MyException("学科成绩已存在", "123");
        }
        return studentCurriculumFractionMapper.insert(studentCurriculumFraction);
    }

    @Override
    public int update(StudentCurriculumFraction studentCurriculumFraction) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", studentCurriculumFraction.getId());
        StudentCurriculumFraction fraction = studentCurriculumFractionMapper.selectOne(queryWrapper);
        if (fraction == null) {
            throw new MyException("学科成绩不存在", "123");
        }
        return studentCurriculumFractionMapper.updateById(studentCurriculumFraction);
    }

    @Override
    public int delete(Integer id) throws MyException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        StudentCurriculumFraction fraction = studentCurriculumFractionMapper.selectOne(queryWrapper);
        if (fraction == null) {
            throw new MyException("学科成绩不存在", "123");
        }
        return studentCurriculumFractionMapper.deleteById(id);
    }

    @Override
    public List<StudentCurriculumFraction> all() {
        return studentCurriculumFractionMapper.all();
    }


}
